<?php
/**
 * Created by PhpStorm.
 * User: 睿
 * Date: 2017-11-10
 * Time: 14:39
 */

namespace app\store\controller;

use controller\BasicAdmin;
use service\DataService;
use think\Db;

class Stock extends BasicAdmin
{
    /**
     * 指定当前数据表
     * @var string
     */
    public $table = 'StoreMaterialStock';

    /**
     * 库存列表
     * @return array
     */
    public function index()
    {
        $get = $this->request->get();

        $store_id = session('user')['store_id'];

        $join = [
            ['ys_goods_material g', 'a.material_id=g.id', 'LEFT'],
            ['ys_goods_material_category c', 'g.cat_id=c.id', 'LEFT'],
        ];

        $db = Db::name($this->table)->alias('a')->join($join)->where('store_id', $store_id)->field('g.name,a.stock,a.warn_stock,a.id,g.unit,a.material_id');

        $warn = db($this->table)->select();
        $num = 0;//报警
        foreach ($warn as $value)
            if ($value['stock'] < $value['warn_stock'])
                $num++;

        if ($num > 0) {
            $this->assign('num', $num);
        }

        $this->title = '原料库存';

        if (isset($get['name']) && $get['name'] !== '')
            $db->whereLike('g.name', '%' . $get['name'] . '%');

        return parent::_list($db);
    }


    /**
     * 移动清单
     * @return array|string
     */
    public function li()
    {
        $id = input('id');

        $this->title = input('title');

        $db = Db::name('store_material_log')->order('id','desc')->where('material_id', $id);

        return parent::_list($db);
    }


    /**
     * 设置安排库存
     * @return array|string
     */
    public function edit()
    {
        return $this->_form($this->table, 'form');
    }

    /**
     * 库存列表
     * @return array
     */
    public function warn()
    {
        $get = $this->request->get();

        $store_id = session('user')['store_id'];

        $join = [
            ['ys_goods_material g', 'a.material_id=g.id', 'LEFT'],
            ['ys_goods_material_category c', 'g.cat_id=c.id', 'LEFT'],
        ];

        $db = Db::name($this->table)->alias('a')->join($join)->where('store_id', $store_id)->field('g.name,a.stock,a.warn_stock,a.id,g.unit,a.material_id');

        $this->title = '报警库存';

        if (isset($get['name']) && $get['name'] !== '')
            $db->whereLike('g.name', '%' . $get['name'] . '%');

        return parent::_list($db);
    }

    /**
     * 报损
     * @return array|string
     */
    public function loss()
    {
        $store_id = session('user')['store_id'];
        if (request()->ispost()) {
            $post = input('post.');
            halt($post);
            $material_stock = db('store_material_stock')->find($post['id']);
            $stock = db('store_material_stock')->where('id',$post['id'])->setDec('stock',$post['loss']);
            $log = db('store_material_log')->insert(['store_id'=>$store_id , 'material_id'=>$material_stock['material_id'] , 'sunhao_num'=>$post['loss'] , 'cur_stock'=>$material_stock['stock']-$post['loss'] , 'addtime'=>time() , 'remark'=>$post['reason']]);
            if ($stock !== false && $log !== false)
                $this->success('原料报损成功！','');
        } else {
            return $this->_form($this->table, 'loss');
        }
    }


}